package com.harassment.interception.ui.fragments.bottomFragment;

import android.animation.ObjectAnimator;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.design.widget.CoordinatorLayout;
import android.support.v4.view.ViewCompat;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

public class BottomNavigationBehavior extends CoordinatorLayout.Behavior<View> {
    private static final String TAG = "BottomNavigationBehavio";
    private ObjectAnimator mOutAnimator;
    private ObjectAnimator mInAnimator;

    public BottomNavigationBehavior(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
    }

    @Override
    public boolean layoutDependsOn(CoordinatorLayout parent, View child, View dependency) {
        return dependency instanceof BottomNavigationView;
    }

    @Override
    public boolean onDependentViewChanged(CoordinatorLayout parent, View child, View dependency) {
        float scaleY = Math.abs(dependency.getY())/dependency.getHeight();
        child.setTranslationY(child.getHeight() * scaleY);
        return true;
    }

    @Override
    public boolean onStartNestedScroll(@NonNull CoordinatorLayout coordinatorLayout, @NonNull View child, @NonNull View directTargetChild, @NonNull View target, int axes, int type) {
        return axes == ViewCompat.SCROLL_AXIS_VERTICAL;
    }

    @Override
    public void onNestedPreScroll(@NonNull CoordinatorLayout coordinatorLayout, @NonNull View child, @NonNull View target, int dx, int dy, @NonNull int[] consumed, int type) {
        if (dy > 0) {
            if (mOutAnimator == null) {
                mOutAnimator = ObjectAnimator.ofFloat(child, "translationY", 0, child.getHeight());
                mOutAnimator.setDuration(500);
            }
            if (!mOutAnimator.isRunning() && child.getTranslationY() <= 0) {
                mOutAnimator.start();
            }
        } else if (dy < 0) {
            if (mInAnimator == null) {
                mInAnimator = ObjectAnimator.ofFloat(child, "translationY", child.getHeight(), 0);
                mInAnimator.setDuration(500);
            }
            if (!mInAnimator.isRunning() && child.getTranslationY() >= child.getHeight()) {
                mInAnimator.start();
            }
        }
    }
}
